package com.tonyodev.fetch2core;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 数据类
 *
 * @since 2021-05-20
 */
public class MutableExtras extends Extras implements Serializable {
    private static final String KEY_VALUE = "key";
    @NotNull
    protected Map mutableData;

    /**
     * 构造函数
     *
     * @param data 数据
     */
    public MutableExtras(LinkedHashMap data) {
        super(data);
        this.mutableData = data;
    }

    /**
     * 构造函数
     */
    public MutableExtras() {
        super();
        mutableData = new LinkedHashMap();
    }

    /**
     * Stores a int value with its key inside this extras object.
     *
     * @param key key used to retrieve value.
     * @param value int value data
     */
    public final void putInt(@NotNull String key, int value) {
        Intrinsics.checkParameterIsNotNull(key, KEY_VALUE);
        this.mutableData.put(key, String.valueOf(value));
    }

    /**
     * Stores a string value with its key inside this extras object
     *
     * @param key key used to retrieve value.
     * @param value string value data
     */
    public final void putString(@NotNull String key, @NotNull String value) {
        Intrinsics.checkParameterIsNotNull(key, KEY_VALUE);
        Intrinsics.checkParameterIsNotNull(value, "value");
        this.mutableData.put(key, String.valueOf(value));
    }

    /**
     * Stores a long value with its key inside this extras object.
     *
     * @param key key used to retrieve value.
     * @param value long value data
     */
    public final void putLong(@NotNull String key, long value) {
        Intrinsics.checkParameterIsNotNull(key, KEY_VALUE);
        this.mutableData.put(key, String.valueOf(value));
    }

    /**
     * Stores a double value with its key inside this extras object.
     *
     * @param key key used to retrieve value.
     * @param value double value data
     */
    public final void putDouble(@NotNull String key, double value) {
        Intrinsics.checkParameterIsNotNull(key, KEY_VALUE);
        this.mutableData.put(key, String.valueOf(value));
    }

    /**
     * Stores a float value with its key inside this extras object.
     *
     * @param key key used to retrieve value.
     * @param value float value data
     */
    public final void putFloat(@NotNull String key, float value) {
        Intrinsics.checkParameterIsNotNull(key, KEY_VALUE);
        this.mutableData.put(key, String.valueOf(value));
    }

    /**
     * Stores a boolean value with its key inside this extras object.
     *
     * @param key key used to retrieve value.
     * @param value boolean value data
     */
    public final void putBoolean(@NotNull String key, boolean value) {
        Intrinsics.checkParameterIsNotNull(key, KEY_VALUE);
        this.mutableData.put(key, String.valueOf(value));
    }

    /**
     * 清空数据
     */
    public final void clear() {
        this.mutableData.clear();
    }

    /**
     * 转成Extras对象
     *
     * @return Extras
     */
    @NotNull
    public final Extras toExtras() {
        return new Extras(getMutableMap());
    }

    /**
     * The backing map that holds the custom key value pairs data.
     *
     * @return mutableData
     */
    public final Map getMutableMap() {
        if (mutableData == null || mutableData.size() == 0) {
            return Collections.emptyMap();
        }
        if (mutableData.size() == 1) {
            Map.Entry entry = (Map.Entry) mutableData.entrySet().iterator().next();
            return Collections.singletonMap(entry.getKey(), entry.getValue());
        } else {
            return mutableData;
        }
    }

    /**
     * 获取mutableData
     *
     * @return mutableData
     */
    public Map getMutableData() {
        return mutableData;
    }
}
